// 引入 Mock
var Mock = require('mockjs')
// 定义数据类型
var data = Mock.mock({
  'users|9': [{
    // Id
    'id|+1': 500,
    'role_name|1': ['超级管理员', '测试角色', '管理员'],
    // 邮箱：
    email: '@email',
    // name
    username: '@cname',
    phone: /^1[385][1-9]\d{8}/,
    'mg_state|1': [true, false]
  }],
  meta: {
    msg: '获取用户列表成功',
    status: 200
  }
})

// 获取用户列表请求
Mock.mock('/api/userlist', 'post', (params) => {
  var total = data.users.length
  var info = JSON.parse(params.body)
  // index 当前页数  size 每页显示条数
  var [index, size] = [info.pageIndex, info.pageSize]
  const start = (index - 1) * size
  const end = index * size
  const newdata = data.users.slice(start, end)
  return {
    pageIndex: index,
    pageSize: size,
    total: total,
    data: newdata
  }
})

// 添加用户请求
Mock.mock('/api/adduser', 'post', (option) => {
  var info = JSON.parse(option.body)
  const uname = info.name
  // const upwd = info.password
  const uemail = info.email
  const umobile = info.mobile
  var addlist = { 'id|+1': 600, email: uemail, username: uname, phone: umobile, 'mg_state|1': [true, false] }
  var newData = data.users.push(addlist)
  return {
    newData,
    meta: {
      msg: '用户创建成功',
      status: 201
    }
  }
})

// 删除用户请求
Mock.mock('/api/deleteuser', 'post', (option) => {
  var infoId = JSON.parse(option.body)
  data.users = data.users.filter(item => item.id !== infoId)
  return {
    meta: {
      msg: '删除成功',
      status: 200
    }
  }
})